import type { FormInstance } from 'ant-design-vue';
import { onMounted, reactive, toRefs } from 'vue';

type PropsType = {
  searchParam: {
    [key: string]: any;
  };
  searchInitParam: {
    [key: string]: any;
  };
}

function useFormc (){
  const state = reactive<PropsType>({
    searchParam: {},
    searchInitParam: {},
  });

  const reset = (formRef?: FormInstance | undefined) => {
    if (formRef) formRef.resetFields();

    state.searchParam = {};
    Object.keys(state.searchInitParam).forEach((key) => {
      state.searchParam[key] = state.searchInitParam[key];
    });
  };

  const setSearch = (key:string,value:any)=>{
    state.searchParam[key] = value
  }

  onMounted(() => {
    reset();
  });
  return {
    ...toRefs(state),
    reset,
    setSearch
  }
}

export default useFormc